package com.uxwine.view.t;

import com.uxwine.alarm.R;
import com.uxwine.audio.AudioEffect;
import com.uxwine.view.t.MarkerLayer.MarkerEventListener;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.text.BoringLayout.Metrics;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class DialView extends FrameLayout {
	
	ImageView mivBkgnd;
	DialLayer mdvDialLayer;
	MarkerLayer mMarkerLayer;
	BtnLayer mBtnLayer;
	DialViewAdapter mApdater;
	OnDialClickListener mOnDialClickListener;
	OnDialMarkListener mOnDialMarkListener;
	
	//for calc
	float mfRadiusRate = 450.0f / 568.0f;
	float mfBtnRaiusRate = 212.0f / 568.0f;
	private float mfMarkerRadiusRate = 360.0f / 568.0f;
	private float mfMarkerWidthRate = 80.0f / 568.0f;
	private float mfDensity;
	
	public DialView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	protected void initView(Context context) {
		final LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.view_time_dial, this);
		
		mivBkgnd = (ImageView)findViewById(R.id.time_dial_bkgnd);
		mdvDialLayer = (DialLayer)findViewById(R.id.time_dial_dial);
		mMarkerLayer = (MarkerLayer)findViewById(R.id.time_dial_marker);
		mBtnLayer = (BtnLayer)findViewById(R.id.time_dial_btn);
		
		mdvDialLayer.setRadiusRate(mfRadiusRate);
		mMarkerLayer.setRate(mfMarkerWidthRate, mfMarkerRadiusRate);
		mMarkerLayer.setOnMarkEventListener(new MListener());
		mBtnLayer.setOnClickListener(new MListener());
		
		mfDensity = context.getResources().getDisplayMetrics().density;
	}		
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int measureWidth = measure(widthMeasureSpec);
		int measureHeight = measure(heightMeasureSpec);

		int d = Math.min(measureWidth, measureHeight);
		setMeasuredDimension(d, d);

		int cd = getChildMeasureSpec(d, 0, d);
		measureChildren(cd, cd);
	}
	
	private int measure(int nMeasureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(nMeasureSpec);
		int specSize = MeasureSpec.getSize(nMeasureSpec);

		if (specMode == MeasureSpec.UNSPECIFIED) {
			result = 200;
		} else {
			result = specSize;
		}
		return result;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		if(changed)
		{
			initBtnLayer(right - left, bottom - top);
		}
		super.onLayout(changed, left, top, right, bottom);
	}

	private void initBtnLayer(int width, int heigt)
	{
		ViewGroup.LayoutParams params = mBtnLayer.getLayoutParams();
		if(null == params)
		{
			int wrap = ViewGroup.LayoutParams.WRAP_CONTENT;
			params = new ViewGroup.LayoutParams(wrap, wrap);
		}
		params.height = (int)(heigt * mfBtnRaiusRate);
		params.width = (int)(width * mfBtnRaiusRate);
		mBtnLayer.setLayoutParams(params);
	}
	
	public void updateView()
	{
		if(mApdater != null)
		{
			mBtnLayer.setBitmap(mApdater.getButtonId());
			mMarkerLayer.setBearing(mApdater.getBearing());
			mMarkerLayer.invalidate();
			
			mdvDialLayer.invalidate();
		}
	}
	
	public void setAdpater(DialViewAdapter adapter)
	{
		mApdater = adapter;
		mdvDialLayer.setAdpater(mApdater);
		mBtnLayer.setBitmap(mApdater.getButtonId());
		mApdater.setHostView(this);
	}
	
	public DialViewAdapter getAdapter()
	{
		return mApdater;
	}
	
	public void setOnDialClickListener(OnDialClickListener l)
	{
		mOnDialClickListener = l;
	}
	
	public void setOnDialMarkListener(OnDialMarkListener l)
	{
		mOnDialMarkListener = l;
	}
	
	public static interface OnDialClickListener
	{
		public void onClick(View v);
	}
	
	public static interface OnDialMarkListener
	{
		public void onMarkChanged(int index);
		public void onMarked(int index);
	}
	
	private class MListener implements View.OnClickListener, MarkerEventListener
	{
		@Override
		public void onClick(View v) {
			AudioEffect.getInstance(getContext().getApplicationContext()).play(1);
			if(null != mOnDialClickListener)
				mOnDialClickListener.onClick(DialView.this);
		}

		@Override
		public boolean onMove(float bearing) {
			if(mApdater != null)
			{
				int current = mApdater.getPosition();
				mApdater.setBearing(bearing);
				int next = mApdater.getPosition();
				Log.d("=======", "c=" + current + ", b=" + bearing + ", n=" + next);
				if(current != next)
				{
					Log.i(getClass().getName(), "onMove...");
					AudioEffect.getInstance(getContext().getApplicationContext()).play(0);
					mdvDialLayer.invalidate();
					if(mOnDialMarkListener != null)
						mOnDialMarkListener.onMarkChanged(next);
				}
			}
			return false;
		}

		@Override
		public void onMarked() {
			if(mApdater != null)
			{
				float bearing = mApdater.getBearing();
				bearing = mApdater.getNearBearing(bearing);
				mApdater.setBearing(bearing);
				mApdater.notifyDataChanged();
				if(null != mOnDialMarkListener)
					mOnDialMarkListener.onMarked(mApdater.getPosition());
			}
		}
		
		
	}
}
